package kr.co.dongpang.android;

import static kr.co.dongpang.android.GCMCommonUtilities.SENDER_ID;
import static kr.co.dongpang.android.GCMCommonUtilities.displayMessage;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {

	private static final String TAG = "GCMIntentService";

	public GCMIntentService() {
		super(SENDER_ID);
	}

	/**
	 * Method called on device registered
	 **/
	@Override
	protected void onRegistered(Context context, String registrationId) {
		Log.i(TAG, "Device registered: regId = " + registrationId);
		displayMessage(context, "sender", "id", "name", "message", "goodcd", "goodnm");
		Log.d("userid", LoginNew.userid);
		GCMServerUtilities.register(context, LoginNew.userid, LoginNew.choice, registrationId);
	}

	/**
	 * Method called on device un registred
	 * */
	@Override
	protected void onUnregistered(Context context, String registrationId) {
		Log.i(TAG, "Device unregistered");
		displayMessage(context, getString(R.string.gcm_unregistered), getString(R.string.gcm_unregistered),
				getString(R.string.gcm_unregistered), getString(R.string.gcm_unregistered), getString(R.string.gcm_unregistered),
				getString(R.string.gcm_unregistered));
		GCMServerUtilities.unregister(context, registrationId);
	}

	/**
	 * Method called on Receiving a new message
	 * */
	@Override
	protected void onMessage(Context context, Intent intent) {
		Log.i(TAG, "Received message");
		String sender = intent.getExtras().getString("sender");
		String id = intent.getExtras().getString("id");
		String name = intent.getExtras().getString("name");
		String message = intent.getExtras().getString("message");
		String goodcd = intent.getExtras().getString("goodcd");
		String goodnm = intent.getExtras().getString("goodnm");
		Log.d("", "sender:1:" + sender);

		displayMessage(context, sender, id, name, message, goodcd, goodnm);
		// notifies user
		generateNotification(context, sender, id, name, message, goodcd, goodnm);
	}

	/**
	 * Method called on receiving a deleted message
	 * */
	@Override
	protected void onDeletedMessages(Context context, int total) {
		Log.i(TAG, "Received deleted messages notification");
		String sender = getString(R.string.gcm_deleted, total);
		String id = getString(R.string.gcm_deleted, total);
		String name = getString(R.string.gcm_deleted, total);
		String message = getString(R.string.gcm_deleted, total);
		String goodcd = getString(R.string.gcm_deleted, total);
		String goodnm = getString(R.string.gcm_deleted, total);
		Log.d("", "goodcd:2:" + goodcd);
		displayMessage(context, sender, id, name, message, goodcd, goodnm);
		// notifies user
		generateNotification(context, sender, id, name, message, goodcd, goodnm);
	}

	/**
	 * Method called on Error
	 * */
	@Override
	public void onError(Context context, String errorId) {
		Log.i(TAG, "Received error: " + errorId);
		displayMessage(context, getString(R.string.gcm_error, errorId), getString(R.string.gcm_error, errorId),
				getString(R.string.gcm_error, errorId), getString(R.string.gcm_error, errorId), getString(R.string.gcm_error, errorId),
				getString(R.string.gcm_error, errorId));
	}

	@Override
	protected boolean onRecoverableError(Context context, String errorId) {
		// log message
		Log.i(TAG, "Received recoverable error: " + errorId);
		displayMessage(context, getString(R.string.gcm_recoverable_error, errorId), getString(R.string.gcm_recoverable_error, errorId),
				getString(R.string.gcm_recoverable_error, errorId), getString(R.string.gcm_recoverable_error, errorId),
				getString(R.string.gcm_recoverable_error, errorId), getString(R.string.gcm_recoverable_error, errorId));
		return super.onRecoverableError(context, errorId);
	}

	/**
	 * Issues a notification to inform the user that server has sent a message.
	 */
	@SuppressWarnings("deprecation")
	private static void generateNotification(Context context, String sender, String id, String name, String message, String goodcd,
			String goodnm) {
		int icon = R.drawable.ic_launcher;
		long when = System.currentTimeMillis();
		NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(icon, message, when);

		String title = context.getString(R.string.app_name);

		Log.d("", "goodcd:5:" + goodcd);
		Log.d("", "goodcd:5:" + sender);

		Intent notificationIntent;
		if (sender != null) {

			if (sender.equals("dm")) {
				notificationIntent = new Intent(context, SmTalkDetail.class);
				notificationIntent.putExtra("goodcd", goodcd);
				notificationIntent.putExtra("goodnm", goodnm);
				notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
				notification.setLatestEventInfo(context, title, message, intent);
				notification.flags |= Notification.FLAG_AUTO_CANCEL;
				notification.sound = Uri.parse("android.resource://kr.co.dongpang.android/" + kr.co.dongpang.android.R.raw.talk);
				notification.defaults |= Notification.DEFAULT_VIBRATE;
				notificationManager.notify(0, notification);
			} else if (sender.equals("sm")) {
				notificationIntent = new Intent(context, DmTalkDetail.class);
				notificationIntent.putExtra("goodcd", goodcd);
				notificationIntent.putExtra("goodnm", goodnm);
				notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
				notification.setLatestEventInfo(context, title, message, intent);
				notification.flags |= Notification.FLAG_AUTO_CANCEL;
				notification.sound = Uri.parse("android.resource://kr.co.dongpang.android/" + kr.co.dongpang.android.R.raw.talk);
				notification.defaults |= Notification.DEFAULT_VIBRATE;
				notificationManager.notify(0, notification);
			} else if (sender.equals("order")) {
				Log.d("", "order::::");
				notificationIntent = new Intent(context, MainActivity.class);
				notificationIntent.putExtra("fragmentName", "action_order");
				notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
				notification.setLatestEventInfo(context, title, message, intent);
				notification.flags |= Notification.FLAG_AUTO_CANCEL;
//				notification.defaults |= Notification.DEFAULT_SOUND;
				notification.sound = Uri.parse("android.resource://kr.co.dongpang.android/" + kr.co.dongpang.android.R.raw.dongpang);
				notification.defaults |= Notification.DEFAULT_VIBRATE;
				notificationManager.notify(0, notification);
			} else if (sender.equals("smShop")) {
				notificationIntent = new Intent(context, DmTalkShopDetail.class);
				notificationIntent.putExtra("goodcd", goodcd);
				notificationIntent.putExtra("goodnm", goodnm);
				notificationIntent.putExtra("writernm", id);
				notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
				notification.setLatestEventInfo(context, title, message, intent);
				notification.flags |= Notification.FLAG_AUTO_CANCEL;
				notification.sound = Uri.parse("android.resource://kr.co.dongpang.android/" + kr.co.dongpang.android.R.raw.talk);
				notification.defaults |= Notification.DEFAULT_VIBRATE;
				notificationManager.notify(0, notification);			
			} else if (sender.equals("dmShop")) {
				notificationIntent = new Intent(context, SmTalkShopDetail.class);
				notificationIntent.putExtra("goodcd", goodcd);
				notificationIntent.putExtra("goodnm", goodnm);
				notificationIntent.putExtra("dmId", id);
				notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
				notification.setLatestEventInfo(context, title, message, intent);
				notification.flags |= Notification.FLAG_AUTO_CANCEL;
				notification.sound = Uri.parse("android.resource://kr.co.dongpang.android/" + kr.co.dongpang.android.R.raw.talk);
				notification.defaults |= Notification.DEFAULT_VIBRATE;
				notificationManager.notify(0, notification);

				//context.startService(new Intent(context, TopViewService.class)); 
			} else if (sender.equals("smMyorder")) {
				notificationIntent = new Intent(context, MainActivity.class);
				notificationIntent.putExtra("fragmentName", "action_my_order");
				notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
				notification.setLatestEventInfo(context, title, message, intent);
				notification.flags |= Notification.FLAG_AUTO_CANCEL;
				notification.sound = Uri.parse("android.resource://kr.co.dongpang.android/" + kr.co.dongpang.android.R.raw.dongpang);
				notification.defaults |= Notification.DEFAULT_VIBRATE;
				notificationManager.notify(0, notification);
			} else if (sender.equals("shopNitice")) {
				notificationIntent = new Intent(context, MainActivity.class);
				notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
				notification.setLatestEventInfo(context, title, message, intent);
				notification.flags |= Notification.FLAG_AUTO_CANCEL;
				notification.sound = Uri.parse("android.resource://kr.co.dongpang.android/" + kr.co.dongpang.android.R.raw.dongpang);
				notification.defaults |= Notification.DEFAULT_VIBRATE;
				notificationManager.notify(0, notification);
			} else if (sender.equals("DmPointFragment")) {
				notificationIntent = new Intent(context, MainActivity.class);
				notificationIntent.putExtra("fragmentName", "DmPointFragment");
				notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
				notification.setLatestEventInfo(context, title, message, intent);
				notification.flags |= Notification.FLAG_AUTO_CANCEL;
//				notification.defaults |= Notification.DEFAULT_SOUND;
				notification.sound = Uri.parse("android.resource://kr.co.dongpang.android/" + kr.co.dongpang.android.R.raw.dongpang);
				notification.defaults |= Notification.DEFAULT_VIBRATE;
				notificationManager.notify(0, notification);				
			}
			
		}

	}

}
